
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * Class: UI
 * regelt de User Interface
 * @author frankkie
 */
public class UI extends JFrame implements ActionListener {

  JPanel buttonPanel;
  JButton btVerbinding;
  JCheckBox cbHost;
  JTextField tfStatus;
  Spel spel;
  public Grid grid;

  /**
   * Constructor UI
   * Maakt de UI
   * @param spel
   */
  public UI(Spel spel) {
    System.out.println("UI.UI");
    this.spel = spel;
    this.setSize(400, 500);
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLayout(new BorderLayout());
    //
    this.tfStatus = new JTextField();
    this.tfStatus.setEditable(false);
    this.add(tfStatus, BorderLayout.NORTH);
    this.grid = new Grid(spel);
    this.setIgnoreRepaint(false);
    spel.setGrid(this.grid);
    //this.add(this.grid, BorderLayout.CENTER);
    //EXPERIMENTAL_CENTERER
    JPanel koekjePanel = new JPanel();
    koekjePanel.setLayout(new GridLayout());
    koekjePanel.add(this.grid);
    this.add(koekjePanel, BorderLayout.CENTER);
    //EINDE EXPERIMENTAL_CENTERER
    //
    this.buttonPanel = new JPanel();
    this.cbHost = new JCheckBox("host");
    this.btVerbinding = new JButton("start verbinding");
    this.btVerbinding.addActionListener(this);
    this.buttonPanel.setLayout(new FlowLayout());
    this.buttonPanel.add(cbHost);
    this.buttonPanel.add(btVerbinding);
    this.add(this.buttonPanel, BorderLayout.SOUTH);
    //
    this.setVisible(true);
  }

  /**
   * Komt van ActionListener
   * wordt aangeroepen als er op de JButton wordt geklikt
   * @param e
   */
  public void actionPerformed(ActionEvent e) {
    System.out.println("UI.actionPerformed");
    //Geklikt op de startverbinding-knop
    //this.btVerbinding.setEnabled(false);
    //this.cbHost.setEnabled(false);
    this.btVerbinding.setVisible(false);
    this.cbHost.setVisible(false);

    if (this.cbHost.isSelected()) {
      //HOST
      this.setStatus("Wachten tot tegenstander is verbonden...");
      //this.btVerbinding.setText("Wachten op tegenstander...");
      spel.startHost();
      this.buttonPanel.add(new JLabel("<html><font color=\"red\"><b>Dit is jouw Kleur (ROOD)</b></font></html>"));
      this.repaint();
    } else {
      //CLIENT
      this.setStatus("Verbinding maken met host...");
      //this.btVerbinding.setText("Verbinding maken...");
      this.buttonPanel.add(new JLabel("<html><font color=\"green\"><b>Dit is jouw Kleur (GROEN)</b></font></html>"));
      spel.startClient();

      this.setStatus("Jij bent Niet aan de Beurt");
      this.repaint();
    }
  }

  /**
   * Verandert de String in de JTextField
   * @param status (String) de Status
   */
  public void setStatus(String status) {
    System.out.println("UI.setStatus(" + status + ")");
    this.tfStatus.setText(status);
    this.repaint();
  }
}
